package com.example.springbatch_itemreader_js_script;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.adapter.ItemProcessorAdapter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.support.ScriptItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@SpringBootApplication
@EnableBatchProcessing
public class JsAdapterProcessorJob {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Bean
    public FlatFileItemReader<User> userItemReader(){
        return new FlatFileItemReaderBuilder<User>()
                .name("userItemReader")
                .resource(new ClassPathResource("users-validate.txt"))
                .delimited().delimiter("#")
                .names("id", "name", "age")
                .targetType(User.class)
                .build();
    }

    @Bean
    public ItemWriter<User> itemWriter(){
        return new ItemWriter<User>() {
            @Override
            public void write(List<? extends User> items) throws Exception {
                items.forEach(System.err::println);
            }
        };
    }


//    @Bean
//    public BeanValidatingItemProcessor<User> beanValidatingItemProcessor(){
//        BeanValidatingItemProcessor<User> beanValidatingItemProcessor = new BeanValidatingItemProcessor<>();
//        beanValidatingItemProcessor.setFilter(true);  //不满足条件丢弃数据
//
//        return beanValidatingItemProcessor;
//    }

    @Resource(name = "userService")
    public UserServiceImpl userService;
    /**
     * 适配器
     * @return 脚本适配器
     */
    @Bean
    public ScriptItemProcessor<User, User> scriptItemProcessor(){
        ScriptItemProcessor<User, User> adapter = new ScriptItemProcessor<>();
//        // 设置目标类
//        adapter.setTargetObject(userService);
//
//        // 设置执行方法
//        adapter.setTargetMethod("toUppeCase");
        // 直接设置js脚本
        adapter.setScript(new ClassPathResource("userScript.js"));

        return adapter;
    }


    @Bean
    public Step step(){
        return stepBuilderFactory.get("step1")
                .<User, User>chunk(1)
                .reader(userItemReader())
                .processor(scriptItemProcessor())
                .writer(itemWriter())
                .build();

    }

    @Bean
    public Job job(){
        return jobBuilderFactory
                .get("validate-processor-js-script-2")
                .start(step())
                .incrementer(new RunIdIncrementer())
                .build();
    }


    public static void main(String[] args) {
        SpringApplication.run(JsAdapterProcessorJob.class, args);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        System.out.println("uuid = " + uuid);
    }
}
